package com.example.bleuesprit.parkAndroidClient;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.bleuesprit.parkAndroidClient.net.NetUtil;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by bleuesprit on 2015-10-03.
 * ok
 */
public class RegistActivity extends Activity{
    EditText registAccount_Edt;
    EditText registPassword_Edt;
    EditText registPasswordAgain_Edt;
    EditText registLicence_Edt;
    EditText registName_Edt;
    EditText registTel_Edt;
    EditText registEmail_Edt;
    Button registDoregist_Bnt;



    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0x123) {
                // 注册信息返回结果
                String registResult=null;//注册是否成功
                String errorPoint=null;//如果出错是哪种错误
                try {
                    JSONObject jsonObject=new JSONObject(msg.getData().getString("result"));
                    registResult=jsonObject.getString("registResult");
                    errorPoint=jsonObject.getString("errorPoint");

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                if(registResult.equals("succeed")) {
                    Toast.makeText(RegistActivity.this, "succeed", Toast.LENGTH_LONG).show();
                    Intent intent=new Intent(RegistActivity.this,LoginActivity.class);
                    startActivity(intent);
                    finish();
                }
                else
                {
                    switch (errorPoint)
                    {
                        case "accountUsed":
                            Toast.makeText(RegistActivity.this, "用户名已经使用", Toast.LENGTH_LONG).show();
                            registAccount_Edt.setText("");
                            break;
                        case "accountInvalid":
                            Toast.makeText(RegistActivity.this, "用户名必须是英文或数字，不低于4位，不多于20位", Toast.LENGTH_LONG).show();
                            registAccount_Edt.setText("");
                            break;
                        case "pwdInvalid":
                            Toast.makeText(RegistActivity.this, "用密码必须是英文或数字，不低于8位，不多于20位", Toast.LENGTH_LONG).show();
                            registPassword_Edt.setText("");
                            registPasswordAgain_Edt.setText("");
                            break;
                        case "licenceInvalid":
                            Toast.makeText(RegistActivity.this, "车牌不正确", Toast.LENGTH_LONG).show();
                            registLicence_Edt.setText("");
                            break;
                        case "telInvalid":
                            Toast.makeText(RegistActivity.this, "电话不正确", Toast.LENGTH_LONG).show();
                            registTel_Edt.setText("");
                            break;
                        default:
                            Log.e("RegistActivity", "registResult errorPoint invalid");
                            break;
                    }
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        registAccount_Edt=(EditText)findViewById(R.id.registAccount);
        registPassword_Edt=(EditText)findViewById(R.id.registPassword);
        registPasswordAgain_Edt=(EditText)findViewById(R.id.registPasswordAgain);
        registLicence_Edt=(EditText)findViewById(R.id.registLicence);
        registName_Edt=(EditText)findViewById(R.id.registName);
        registTel_Edt=(EditText)findViewById(R.id.registTel);
        registEmail_Edt=(EditText)findViewById(R.id.registEmail);
        registDoregist_Bnt=(Button)findViewById(R.id.registDoregist);

        registDoregist_Bnt.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        doRegist(v);
                    }
                }
        );
    }


    void doRegist(View v)
    {
        if(!registPassword_Edt.getText().toString().equals(registPasswordAgain_Edt.getText().toString()))
        {
            registPassword_Edt.setText("");
            registPasswordAgain_Edt.setText("");
            Toast.makeText(RegistActivity.this,"两次密码输入不同",Toast.LENGTH_SHORT).show();
            return;
        }

        JSONObject registMessage=new JSONObject();
        try {
            registMessage.put("account", registAccount_Edt.getText().toString());
            registMessage.put("pwd", registPassword_Edt.getText().toString());
            registMessage.put("licence", registLicence_Edt.getText().toString());
            registMessage.put("name", registName_Edt.getText().toString());
            registMessage.put("tel", registTel_Edt.getText().toString());
            registMessage.put("email", registEmail_Edt.getText().toString());

        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtil.sendToServer("regist", registMessage, handler);

    }

}
